package com.cqh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqh.common.R;
import com.cqh.dto.SetmealDto;
import com.cqh.entity.Category;
import com.cqh.entity.Dish;
import com.cqh.entity.Setmeal;
import com.cqh.service.CategotyService;
import com.cqh.service.SetmealDishService;
import com.cqh.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    CategotyService categotyService;
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);

        return R.success("新增成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Setmeal> pageinfo=new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage=new Page<>();

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name!=null,Setmeal::getName,name);
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageinfo,lambdaQueryWrapper);
        BeanUtils.copyProperties(pageinfo,setmealDtoPage,"records");
        List<Setmeal> setmeals=pageinfo.getRecords();
       List<SetmealDto> list= setmeals.stream().map((item)->{
            Long categoryId=item.getCategoryId();
            SetmealDto setmealDto=new SetmealDto();
            Category category=categotyService.getById(categoryId);
            if (category!=null){
                String categoryname=category.getName();
                setmealDto.setCategoryName(categoryname);
            }
            BeanUtils.copyProperties(item,setmealDto);
           return  setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
            setmealService.removeWithDish(ids);
           return R.success("删除成功");
    }
    @PostMapping("/status/0")
    public R<String> status0(@RequestParam List<Long> ids){

            List<Setmeal> list=setmealService.listByIds(ids);
            list.stream().map((item)->{

                item.setStatus(0);
                return item;
            }).collect(Collectors.toList());
            setmealService.saveOrUpdateBatch(list);
            return R.success("改变成功");
    }
    @PostMapping("/status/1")
    public R<String> status1(@RequestParam List<Long> ids){

        List<Setmeal> list=setmealService.listByIds(ids);
        list.stream().map((item)->{

            item.setStatus(1);
            return item;
        }).collect(Collectors.toList());
        setmealService.saveOrUpdateBatch(list);
        return R.success("改变成功");
    }
}
